//@Lim Wei Ke A0083402R
package logic;

import java.util.ArrayList;

import storage.Storage;
import global.CommandType.Type;
import global.Task;

public class AddCommand extends Command {

	private Task task;
	private boolean isExecutable;
	private String feedback;

	
	public AddCommand(Task task, boolean isExecutable, String feedback) {
		this.task = task;
		this.isExecutable = isExecutable;
		this.feedback = feedback;
	}

    public ArrayList<String> execute() {
    	ArrayList<String> newFeedback = new ArrayList<String>();
    	
        if (isExecutable){
                feedback = "Sucessfully Added.";               
                Task taskToBeAdded = null;
                try{
                	taskToBeAdded = task;
                	Storage.addFile(taskToBeAdded);
                	DisplayCommand displayAddedTask = new DisplayCommand(task);
                	newFeedback = displayAddedTask.displayNewlyAddedTask(0);
                	ArrayList<Task> tasksAdded = new ArrayList<Task>();
                	tasksAdded.add(task);
                	ExecutionHandler.setOldTasks(tasksAdded);
                	ExecutionHandler.setIsUndo(false);
                	ExecutionHandler.setMostRecentCommandType(Type.ADD);
                } catch(Exception e){
                	DisplayCommand lastDisplayed = ExecutionHandler.getRecentDisplay();
                	newFeedback = lastDisplayed.display();
                	feedback = ("Adding Task "+ taskToBeAdded.getTaskName() + " Unsuccessful");
                }
        }
        
        newFeedback.add(feedback);
        return newFeedback;
}
    
	public Task getTask(){
		return task;
	}
	
	public boolean isExecutable(){
		return isExecutable;
	}
	
	public String getFeedback() {
		return feedback;
	}

	public String getTestFeedback() {
		String testFeedback = "";
		
		if (isExecutable) {
			Task curTask = task;
			testFeedback += (curTask.getFormattedTaskString());
		} else {
			testFeedback += "fail";
		}
		return testFeedback;
	}
    
}
//@Lim Wei Ke A0083402R